﻿using CMS.Models;
using CMS.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class MemberViewModel : ViewModelBase
    {
        public MemberViewModel()
        {
            members = new MemberProvider().Select();
        }
        public AppData AppData { get; set; } = AppData.Instance;

        private List<Member> members = new List<Member>();
        public List<Member> Members
        {
            get { return members; }
            set
            {
                members = value;
                RaisePropertyChanged();
            }
        }       


        public RelayCommand OpenAddMemberWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddMemberWindow();
                    window.ShowDialog();
                    //刷新数据
                    Members = new MemberProvider().Select();
                });
            }
        }


        /// <summary>
        /// 删除物资命令
        /// </summary>
        public RelayCommand<object> DeleteMemberCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (MessageBox.Show("Delete?", "msg", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    {
                        return;
                    }
                    if (!(arg is Member model)) return;
                    var count = new MemberProvider().Delete(model);
                    if (count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示", "删除完成");
                    }
                    //刷新数据
                    Members = new MemberProvider().Select();
                });
            }
        }
        /// <summary>
        /// 物资命令
        /// </summary>
        public RelayCommand<object> EditCargoCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (!(arg is Member model)) return;

                    var count = new MemberProvider().Update(model);
                    if (count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示", "操作成功");
                    }
                    //刷新数据
                    Members = new MemberProvider().Select();
                });
            }
        }

    }//public class CargoTypeViewModel : ViewModelBase  
}
